﻿
namespace Data.Repository
{
    using System.Linq;
	using System.Collections.Generic;

    public class DocumentRepository : Data.Repository.IDocumentRepository
    {
        private LinqToECommerceDBDataContext _dbContext;

        #region IDocumentRepository Members

        /// <summary>
        /// create new document
        /// </summary>
        /// <param name="theNewDocument">the new document entity</param>
        /// <returns>the new document's ID</returns>
        public int CreateNewDocument(Data.Entity.Document theNewDocument)
        {
            if (theNewDocument == null)
            {
                return 0;
            }

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_Document_Create(theNewDocument.DocumentBinary, theNewDocument.Extension);
			}

			return result;
        }

        /// <summary>
        /// delete document method
        /// </summary>
        /// <param name="id">the delete document's ID</param>
        /// <returns>0: failed 1: successed</returns>
        public int DeleteDocument(int id)
        {
            if (id <= 0)
            {
                return 0;
            }

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_Document_Delete(id);
			}

			return result;
        }

        /// <summary>
        /// delete Product Document Mapping
        /// </summary>
        /// <param name="documentID">the document's ID</param>
        /// <returns>0: failed 1: successed</returns>
        public int DeleteProductDocumentMapping(int documentID)
        {
            if (documentID <= 0)
            {
                return 0;
            }

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ProductDocument_Delete(documentID);
			}

			return result;
        }

        /// <summary>
        /// get document by product method
        /// </summary>
        /// <param name="productID">the product ID</param>
        /// <returns>products</returns>
		public IList<Data.Entity.Document> GetDocumentByProduct(int productID)
		{
			if(productID <= 0)
			{
				return null;
			}

			List<ECommerce_GetDocuments_By_ProductResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetDocuments_By_Product(productID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Document>();

				foreach(var d in lstData)
				{
					var theEntity = new Data.Entity.Document()
					{
						DocumentBinary = d.DocumentBinary,
						DocumentID = d.DocumentID,
						Extension = d.Extension
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

        #endregion
    }
}
